package hip.util.lang;


import hip.util.Base64Utils;
import org.apache.commons.lang3.StringUtils;

public class ParseType {

    public static String toD8(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            boolean isDate = false;
            String error = value + "不是合法日期类型，不能转换成yyyyMMdd格式";
            if (value.length() == 8) {
                String df = "yyyyMMdd";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value;
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 10) {
                String df = "";
                if (value.indexOf("/") == 2) {
                    df = "dd/MM/yyyy";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(6) + value.substring(3, 5) + value.substring(0, 2);
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("-") == 4) {
                    df = "yyyy-MM-dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8);
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("/") == 4) {
                    df = "yyyy/MM/dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8);
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf(".") == 4) {
                    df = "yyyy.MM.dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8);
                    } else {
                        throw new RuntimeException(error);
                    }
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 11) {
                String df = "yyyy年MM月dd日";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10);
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 14) {
                String df = "yyyyMMddHHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 8);
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 15) {
                String df = "yyyyMMdd'T'HHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 8);
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() > 15) {
                String value8 = value.substring(0, 8);
                String df8 = "yyyyMMdd";
                isDate = StringType.isDate(value8, df8);
                if (isDate) {
                    return value;
                } else {
                    String value11 = value.substring(0, 11);
                    String df11 = "yyyy年MM月dd日";
                    isDate = StringType.isDate(value11, df11);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10);
                    } else {
                        String value10 = value.substring(0, 10);
                        return toD8(value10);
                    }
                }
            } else {
                throw new RuntimeException(error);
            }
        }
        return null;
    }

    public static String toD14(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            boolean isDate = false;
            String error = value + "不是合法日期类型，不能转换成yyyyMMddHHmmss格式";
            if (value.length() == 8) {
                String df = "yyyyMMdd";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value + "000001";
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 10) {
                String df = "";
                if (value.indexOf("/") == 2) {
                    df = "dd/MM/yyyy";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(6) + value.substring(3, 5) + value.substring(0, 2) + "000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("-") == 4) {
                    df = "yyyy-MM-dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("/") == 4) {
                    df = "yyyy/MM/dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf(".") == 4) {
                    df = "yyyy.MM.dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 11) {
                String df = "yyyy年MM月dd日";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10) + "000001";
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 14) {
                String df = "yyyyMMddHHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value;
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 15) {
                String df = "yyyyMMdd'T'HHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 8) + value.substring(9);
                } else {
                    String df15 = "yyyyMMdd HHmmss";
                    isDate = StringType.isDate(value, df15);
                    if (isDate) {
                        return value.substring(0, 8) + value.substring(9);
                    } else {
                        throw new RuntimeException(error);
                    }
                }
            } else if (value.length() == 19) {
                String dfT = "yyyy-MM-dd'T'HH:mm:ss";
                isDate = StringType.isDate(value, dfT);
                if (isDate) {
                    return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10) + value.substring(11, 13) + value.substring(14, 16) + value.substring(17);
                } else {
                    String dfB = "yyyy-MM-dd HH:mm:ss";
                    isDate = StringType.isDate(value, dfB);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10) + value.substring(11, 13) + value.substring(14, 16) + value.substring(17);
                    } else {
                        throw new RuntimeException(error);
                    }
                }
            } else if (value.length() > 14) {
                String value14 = value.substring(0, 14);
                String df14 = "yyyyMMddHHmmss";
                isDate = StringType.isDate(value14, df14);
                if (isDate) {
                    return value;
                } else {
                    String value15 = value.substring(0, 15);
                    try {
                        return toD14(value15);
                    } catch (Exception e) {
                        String value19 = value.substring(0, 19);
                        return toD14(value19);
                    }
                }
            } else {
                throw new RuntimeException(error);
            }
        }
        return null;
    }

    public static String toDT15(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            boolean isDate = false;
            String error = value + "不是合法日期类型，不能转换成yyyyMMdd'T'HHmmss格式";
            if (value.length() == 8) {
                String df = "yyyyMMdd";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value + "T000001";
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 10) {
                String df = "";
                if (value.indexOf("/") == 2) {
                    df = "dd/MM/yyyy";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(6) + value.substring(3, 5) + value.substring(0, 2) + "T000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("-") == 4) {
                    df = "yyyy-MM-dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "T000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf("/") == 4) {
                    df = "yyyy/MM/dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "T000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else if (value.indexOf(".") == 4) {
                    df = "yyyy.MM.dd";
                    isDate = StringType.isDate(value, df);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8) + "T000001";
                    } else {
                        throw new RuntimeException(error);
                    }
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 11) {
                String df = "yyyy年MM月dd日";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10) + "T000001";
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 14) {
                String df = "yyyyMMddHHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value.substring(0, 8) + "T" + value.substring(8);
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 15) {
                String df = "yyyyMMdd'T'HHmmss";
                isDate = StringType.isDate(value, df);
                if (isDate) {
                    return value;
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 19) {
                String dfT = "yyyy-MM-dd'T'HH:mm:ss";
                isDate = StringType.isDate(value, dfT);
                if (isDate) {
                    return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 13) + value.substring(14, 16) + value.substring(17);
                } else {
                    String dfB = "yyyy-MM-dd HH:mm:ss";
                    isDate = StringType.isDate(value, dfB);
                    if (isDate) {
                        return value.substring(0, 4) + value.substring(5, 7) + value.substring(8, 10) + "T" + value.substring(11, 13) + value.substring(14, 16) + value.substring(17);
                    } else {
                        throw new RuntimeException(error);
                    }
                }
            } else if (value.length() > 15) {
                String value15 = value.substring(0, 15);
                String df15 = "yyyyMMdd'T'HHmmss";
                isDate = StringType.isDate(value15, df15);
                if (isDate) {
                    return value;
                } else {
                    String value14 = value.substring(0, 14);
                    String df14 = "yyyyMMddHHmmss";
                    isDate = StringType.isDate(value14, df14);
                    if (isDate) {
                        return value.substring(0, 8) + "T" + value.substring(8);
                    } else {
                        String value19 = value.substring(0, 19);
                        return toDT15(value19);
                    }
                }
            } else {
                throw new RuntimeException(error);
            }
        }
        return null;
    }

    public static String toT6(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            boolean isTime = false;
            String error = value + "不是合法日期类型，不能转换成HHmmss格式";
            if (value.length() == 6) {
                String df = "HHmmss";
                isTime = StringType.isDate(value, df);
                if (isTime) {
                    return value;
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() == 8) {
                String df = "";
                if (value.indexOf(":") == 2) {
                    df = "HH:mm:ss";
                } else if (value.indexOf("时") == 2) {
                    df = "HH时mm分ss";
                }
                isTime = StringType.isDate(value, df);
                if (isTime) {
                    return value.substring(0, 2) + value.substring(3, 5) + value.substring(6);
                } else {
                    throw new RuntimeException(error);
                }
            } else if (value.length() > 8) {
                String value6 = value.substring(0, 6);
                String df6 = "HHmmss";
                isTime = StringType.isDate(value6, df6);
                if (isTime) {
                    return value;
                } else {
                    String value8 = value.substring(0, 8);
                    return toT6(value8);
                }
            } else {
                throw new RuntimeException(error);
            }
        }
        return null;
    }

    public static String toT6(String value, String fromType) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            if (fromType.equalsIgnoreCase("DT15")) {
                String dt15 = toDT15(value);
                return dt15.substring(10);
            } else if (fromType.equalsIgnoreCase("D14")) {
                String d14 = toD14(value);
                return d14.substring(9);
            } else {
                return value;
            }
        }
        return null;
    }

    public static String toBL_0Or1(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            if ("true".equalsIgnoreCase(value)) {
                return "1";
            } else if ("false".equalsIgnoreCase(value)) {
                return "0";
            } else if ("0".equals(value)) {
                return "0";
            } else if ("1".equals(value)) {
                return "1";
            } else if ("Y".equalsIgnoreCase(value)) {
                return "1";
            } else if ("N".equalsIgnoreCase(value)) {
                return "0";
            } else {
                throw new RuntimeException(value + "不是合法的布尔类型，不能转换成0|1格式");
            }
        }
        return null;
    }

    public static String toBL_tOrf(String value) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            if ("1".equals(value)) {
                return "true";
            } else if ("0".equals(value)) {
                return "false";
            } else if ("true".equalsIgnoreCase(value)) {
                return "true";
            } else if ("false".equalsIgnoreCase(value)) {
                return "false";
            } else if ("有".equalsIgnoreCase(value)) {
                return "true";
            } else if ("无".equalsIgnoreCase(value)) {
                return "false";
            } else if ("Y".equalsIgnoreCase(value)) {
                return "true";
            } else if ("N".equalsIgnoreCase(value)) {
                return "false";
            } else {
                throw new RuntimeException(value + "不是合法的布尔类型，不能转换成true|false格式");
            }
        }
        return null;
    }

    public static String toBase64(String value, String charset) {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            boolean isBY = StringType.isBase64(value, charset);
            if (isBY) {
                return value;
            } else {
                return Base64Utils.base64Escape(value, charset);
            }
        }
        return null;
    }

    public static String toDecimal(String value, int decimalSize, int intMin, int intMax) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            StringBuilder sb = new StringBuilder();
            String[] ss = value.split("\\.");
            int intS = ss[0].length();
            if (intS > intMax) {
                throw new RuntimeException(value + "整数位的长度不在" + intMin + "~" + intMax + "长度范围内");
            } else if (intS < intMin) {//前面补0
                int completePos = intMin - intS;
                for (int i = 0; i < completePos; i++) {
                    sb.append("0");
                }
            }

            int decimalS;
            if (ss.length == 2) {
                decimalS = ss[1].length();
            } else {
                decimalS = 0;
            }
            int difS = decimalS - decimalSize;
            if (difS < 0) {//小数位不够补0
                if (value.indexOf(".") != -1) {
                    sb.append(value);
                } else {
                    sb.append(value + ".");
                }
                int abs = Math.abs(difS);
                for (int i = 0; i < abs; i++) {
                    sb.append("0");
                }
                return sb.toString();
            } else if (difS > 0) {//小数位超出则截除
                sb.append(value);
                return sb.toString().substring(0, sb.length() - difS);
				/*BigDecimal db = BigDecimal.valueOf(Double.parseDouble(value));
				double doubleVal = db.setScale(decimalSize).doubleValue();//默认四舍五入
				return String.valueOf(doubleVal);*/
            } else {
                sb.append(value);
                return sb.toString();
            }
        }
        return null;
    }

    public static String toDecimal(String value, int decimalSize, int intSize) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            StringBuilder sb = new StringBuilder();
            String[] ss = value.split("\\.");
            int intS = ss[0].length();
            if (intS > intSize) {
                throw new RuntimeException(value + "整数位的长度不等于" + intSize);
            } else if (intS < intSize) {
                int completePos = intSize - intS;
                for (int i = 0; i < completePos; i++) {
                    sb.append("0");
                }
            }

            int decimalS;
            if (ss.length == 2) {
                decimalS = ss[1].length();
            } else {
                decimalS = 0;
            }
            int difS = decimalS - decimalSize;
            if (difS < 0) {//小数位不够补0
                if (value.indexOf(".") != -1) {
                    sb.append(value);
                } else {
                    sb.append(value + ".");
                }
                int abs = Math.abs(difS);
                for (int i = 0; i < abs; i++) {
                    sb.append("0");
                }
                return sb.toString();
            } else if (difS > 0) {//小数位超出则截除
                sb.append(value);
                return sb.toString().substring(0, sb.length() - difS);
				/*BigDecimal db = BigDecimal.valueOf(Double.parseDouble(value));
				double doubleVal = db.setScale(decimalSize).doubleValue();//默认四舍五入
				return String.valueOf(doubleVal);*/
            } else {
                sb.append(value);
                return sb.toString();
            }
        }
        return null;
    }

    public static String toInt(String value, int intMin, int intMax) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            StringBuilder sb = new StringBuilder();
            String[] ss = value.split("\\.");
            int intS = ss[0].length();
            if (intS > intMax) {
                throw new RuntimeException(value + "整数位的长度不在" + intMin + "~" + intMax + "范围内");
            } else if (intS < intMin) {
                int completePos = intMin - intS;
                for (int i = 0; i < completePos; i++) {
                    sb.append("0");
                }
            }

            if (ss.length == 2) {
                sb.append(ss[0]);
                //double rint = Math.floor(Double.parseDouble(value));
                return sb.toString();
            } else {
                sb.append(value);
                return sb.toString();
            }
        }
        return null;
    }

    public static String toInt(String value, int intSize) throws Exception {
        if (StringUtils.isNotBlank(value) && !"null".equals(value)) {
            StringBuilder sb = new StringBuilder();
            String[] ss = value.split("\\.");
            int intS = ss[0].length();
            if (intS > intSize) {
                throw new RuntimeException(value + "长度不等于" + intSize);
            } else if (intS < intSize) {
                int completePos = intSize - intS;
                for (int i = 0; i < completePos; i++) {
                    sb.append("0");
                }
            }

            if (ss.length == 2) {
                sb.append(ss[0]);
                //double rint = Math.floor(Double.parseDouble(value));
                return sb.toString();
            } else {
                sb.append(value);
                return sb.toString();
            }
        }
        return null;
    }
}
